# Maintainer: Marian Buschsieweke <marian.buschsieweke@posteo.net>
pkgname=nextpnr
pkgver=0.7
pkgrel=0
pkgdesc="portable FPGA place and route tool"
url="https://github.com/YosysHQ/nextpnr"
arch="all"
license="ISC"
makedepends="
	py3-apicula
	boost-dev
	cmake
	eigen-dev
	icestorm
	ninja
	prjtrellis
	prjtrellis-db-ecp5
	python3-dev
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/YosysHQ/nextpnr/archive/refs/tags/nextpnr-$pkgver.tar.gz
	"
builddir="$srcdir/nextpnr-nextpnr-$pkgver"

_fpga_archs="generic ice40 ecp5 gowin"
for _fpga_arch in $_fpga_archs; do
	subpackages="$subpackages $pkgname-$_fpga_arch:_subpkg"
done

build() {
	for _fpga_arch in $_fpga_archs; do
		echo "--> building for $_fpga_arch"
		# regarding -DUSE_IPO=off: see https://github.com/YosysHQ/nextpnr/issues/797
		cmake -B "build-$_fpga_arch" -G Ninja \
			-DBUILD_SHARED_LIBS=True \
			-DCMAKE_BUILD_TYPE=MinSizeRel \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DUSE_IPO=off \
			-DBUILD_TESTS=ON \
			-DCURRENT_GIT_VERSION=$pkgver \
			-DARCH="$_fpga_arch" \
			#
		cmake --build build-$_fpga_arch
	done
}

check() {
	for _fpga_arch in $_fpga_archs; do
		cd "$builddir/build-$_fpga_arch"
		CTEST_OUTPUT_ON_FAILURE=TRUE ctest -j1
	done
}

package() {
	for _fpga_arch in $_fpga_archs; do
		depends="$depends $pkgname-$_fpga_arch"
	done
	mkdir -p "$pkgdir"
}

_subpkg() {
	local _fpga_arch="${subpkgname#nextpnr-}"
	pkgdesc="portable FPGA place and route tool for $_fpga_arch"
	DESTDIR="$subpkgdir" cmake --install "$builddir/build-$_fpga_arch"
}

sha512sums="
feb190d3a656c7bcdd2bcc1def9972e2c9bec7171a064308b16260240ce9f552eb03d907ef7d032a17ae7ef7e869950e7399c61df22ba36484a4cf2ef7ce7de2  nextpnr-0.7.tar.gz
"
